﻿//#define Dragon
#define Debug
#define Trace
#if (Debug && Trace)
#define DebugAndTrace
#endif
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformReflection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ServiceReference1.MobileCodeWSSoapClient client = new ServiceReference1.MobileCodeWSSoapClient();
            string ss = client.getMobileCodeInfo("", "");
        }

        private void btnReflection_Click(object sender, EventArgs e)
        {
            MemberInfo info = typeof(MyClass);
            object[] attributes = info.GetCustomAttributes(true);
            for (int i = 0; i < attributes.Length; i++)
            {
                Console.WriteLine(attributes[i]);
            }
        }

        private void btnReflection2_Click(object sender, EventArgs e)
        {
            Rectangle r = new Rectangle(4.5, 7.5);
            r.Display();
            Type type = typeof(Rectangle);
            // 遍历 Rectangle 类的属性
            foreach (Object attributes in type.GetCustomAttributes(false))
            {
                DeBugInfo dbi = (DeBugInfo)attributes;
                if (null != dbi)
                {
                    Console.WriteLine("Bug no: {0}", dbi.BugNo);
                    Console.WriteLine("Developer: {0}", dbi.Developer);
                    Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                    Console.WriteLine("Remarks: {0}", dbi.Message);
                }
            }

            // 遍历方法属性
            foreach (MethodInfo m in type.GetMethods())
            {
                foreach (Attribute a in m.GetCustomAttributes(true))
                {
                    DeBugInfo dbi = a as DeBugInfo;
                    if (null != dbi)
                    {
                        Console.WriteLine("Bug no: {0}, for Method: {1}",dbi.BugNo, m.Name);
                        Console.WriteLine("Developer: {0}", dbi.Developer);
                        Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                        Console.WriteLine("Remarks: {0}", dbi.Message);
                    }
                }
            }
            Console.ReadLine();
        }

        private void btnAttribute1_Click(object sender, EventArgs e)
        {
            Myclass.Message("In Main function.");
            Test.function1();
            Console.ReadKey();
        }

        private void btnCondional_Click(object sender, EventArgs e)
        {
//#if Dragon
//            Console.WriteLine("Dragon is defined.");
//#else
//            Console.WriteLine("Dragon is not defined.");
//#endif

            Print_1();
            Print_2();
            Print_3();
            Print_4();
            OldMethod();
        }


        [Conditional("Debug")]
        static void Print_1()
        {
            Console.WriteLine("Debug is defined");
        }

        [Conditional("DEBUG")]
        static void Print_2()
        {
            Console.WriteLine("DEBUG is defined");

        }
        //只有定义了Debug或者Tarce后才会执行此方法
        [Conditional("Debug"), Conditional("Trace")]
        static void Print_3()
        {
            Console.WriteLine("Debug or Trace is defined");
        }
        //只有定义了Debug和Trace后才会执行此方法
        [Conditional("DebugAndTrace")]
        static void Print_4()
        {
            Console.WriteLine("Debug and Trace is defined");
        }

        [Obsolete("Don't use OldMethod, use NewMethod instead", false)]
        static void OldMethod()
        {
            Console.WriteLine("It is the old method");
        }
        static void NewMethod()
        {
            Console.WriteLine("It is the new method");
        }

        public string Result { get; set; } = "";
        private int index = 0;
        private void btnTestAsync_Click(object sender, EventArgs e)
        {
            //调用函数开始执行
            Console.WriteLine(string.Format("{0}:开始测试\n", ++index));
            QueryMsgByGetAsync();
            //从异步方法中返回了
            Console.WriteLine(string.Format("{0}:结束测试函数\n", ++index));
        }
        public async void QueryMsgByGetAsync()
        {
            Console.WriteLine(string.Format("{0}:开始执行async函数\n", ++index));
            HttpClient client = new HttpClient
            {
                Timeout = TimeSpan.FromSeconds(30)
            };
            Console.WriteLine(string.Format("{0}:执行await表达式\n", ++index));

            HttpResponseMessage response = await client.GetAsync("http://localhost:31154/api/values");

            //await表达式完成后返回该处继续执行
            Console.WriteLine(string.Format("{0}:await表达式执行完成\n", ++index));
        }
        private void ShowRstBtn_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Result);
        }
    }

    public class Myclass
    {
        [Conditional("DEBUG1")]
        public static void Message(string msg)
        {
            Console.WriteLine(msg);
        }

    }
    public class Test
    {
        public static void function1()
        {
            Myclass.Message("In Function 1.");
            function2();
        }
        public static void function2()
        {
            Myclass.Message("In Function 2.");
        }
    }

    [AttributeUsage(AttributeTargets.All)]
    public class HelpAttribute : Attribute
    {
        public readonly string Url;
        private string topic;
        // Topic 是一个命名（named）参数
        public string Topic
        {
            get
            {
                return topic;
            }
            set
            {
                topic = value;
            }
        }

        public HelpAttribute(string url)  // url 是一个定位（positional）参数
        {
            this.Url = url;
        }
    }
    [HelpAttribute("Information on the class MyClass")]
    class MyClass
    {

    }


    public class DeBugInfo : Attribute
    {
        private int bugNo;
        private string developer;
        private string lastReview;
        public string message;

        public DeBugInfo(int bg, string dev, string d)
        {
            this.bugNo = bg;
            this.developer = dev;
            this.lastReview = d;
        }

        public int BugNo
        {
            get
            {
                return bugNo;
            }
        }
        public string Developer
        {
            get
            {
                return developer;
            }
        }
        public string LastReview
        {
            get
            {
                return lastReview;
            }
        }
        public string Message
        {
            get
            {
                return message;
            }
            set
            {
                message = value;
            }
        }
    }
    [DeBugInfo(45, "Zara Ali", "12/08/2012", Message = "Return type mismatch")]
    //[DeBugInfo(49, "Nuha Ali", "10/10/2012",  Message = "Unused variable")]
    class Rectangle
    {
        // 成员变量
        protected double length;
        protected double width;
        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }
        [DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
        public double GetArea()
        {
            return length * width;
        }
        [DeBugInfo(56, "Zara Ali", "19/10/2012")]
        public void Display()
        {
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }
    }//end class Rectangle  

}
